<?php
/**
 * 分类管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\cn\controllers;


use app\libs\IeltsController;
use app\libs\Method;
use app\libs\VerificationCode;
use yii;
use app\modules\cn\models\Info;
use app\modules\content\models\Active;
use app\modules\content\models\Banner;
use app\modules\content\models\Book;
use app\modules\content\models\Classes;
use app\modules\content\models\Recommend;
use app\modules\content\models\Teachers;
use app\modules\cn\models\CaseList;

class LoginController extends IeltsController
{
    public $enableCsrfValidation = false;
    public $layout = 'cn';

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $detect = new \Mobile_Detect();
        if ($detect->isMobile()){
            $this->redirect('http://m.thinkuprep.com');
        }
    }

    
    
    /**
     * 点击获取验证码
     * @sjeam
     */
    public function actionVerificationCode(){
        ob_clean();
        $_vc = new VerificationCode();  //实例化一个对象
        $_vc->doimg();
        Yii::$app->session->set('verificationCode',$_vc->getCode());//验证码保存到SESSION中
    }


    /**
     * 登录
     * @SEJAM
     */
    public function actionIndex(){
        Yii::$app->session->set('phoneTime',time());
        $url = Yii::$app->session->get('url');
        $source = Yii::$app->session->get('source');
        $userPass = isset($_COOKIE['readPass'])?$_COOKIE['readPass']:'';
        $userName= isset($_COOKIE['readName'])?$_COOKIE['readName']:'';
        $readSign= isset($_COOKIE['readSign'])?$_COOKIE['readSign']:'';

        $userPhone= isset($_COOKIE['userPhone'])?$_COOKIE['userPhone']:'';
        $readZh= isset($_COOKIE['readZh'])?$_COOKIE['readZh']:'';
        // var_dump($source);die;
        // var_dump($_COOKIE);die;
        if($url && $source){
            $url = Yii::$app->request->get('url');
            $source = Yii::$app->request->get('source');
            if($url && $source){
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('login',['readSign' => $readSign,'userPass' => $userPass,'userName' => $userName,'userPhone' => $userPhone,'readZh' => $readZh,'url'=>$url,'source'=>$source]);
            }else {
                return $this->renderPartial('login',['readSign' => $readSign,'userPass' => $userPass,'userName' => $userName,'userPhone' => $userPhone,'readZh' => $readZh,'url'=>$url,'source'=>$source]);
            }
        }else{
            $url = Yii::$app->request->get('url',Yii::$app->params['thinkUrl']);
            $source = Yii::$app->request->get('source',1);
            if(!$url || !$source){
                die('<script>alert("路径错误，请重试");history.go(-1);</script>');
            }else {
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('login',['readSign' => $readSign,'userPass' => $userPass,'userName' => $userName,'userPhone' => $userPhone,'readZh' => $readZh,'url'=>$url,'source'=>$source]);
            }
        }
    }

    public function actionFundpass()
    {
        return $this->renderPartial('foundPassword');
    }

    /**
     * 短信登录
     * @SEJAM
     */
    public function actionPhone(){
        Yii::$app->session->set('phoneTime',time());
        $url = Yii::$app->session->get('url');
        $source = Yii::$app->session->get('source');
        if($url && $source){
            $url = Yii::$app->request->get('url');
            $source = Yii::$app->request->get('source');
            if($url && $source){
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('phone');
            }else {
                return $this->renderPartial('phone');
            }
        }else{
            $url = Yii::$app->request->get('url');
            $source = Yii::$app->request->get('source');
            if(!$url || !$source){
                die('<script>alert("路径错误，请重试");history.go(-1);</script>');
            }else {
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('phone',['url'=>$url,'source'=>$source]);
            }
        }
    }



    /**
     * 注册
     * @SEJAM
     */
    public function actionRegister(){
        $ipInfo = Yii::$app->request->getUserIP();
        $data = Method::getIpInfo($ipInfo);
        $country = $data[0];
        $city = $data[1];
        Yii::$app->session->set('phoneTime',time());
        $url = Yii::$app->session->get('url');
        $source = Yii::$app->session->get('source');
        if($url && $source){
            $session_url = $url;
            $session_source = $source;
            $url = Yii::$app->request->get('url');
            $source = Yii::$app->request->get('source');
            if($url && $source){
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('registered',['country' => $country,'city' => $city, 'url'=>$url, 'source'=>$source]);
            }else {
                return $this->renderPartial('registered',['country' => $country,'city' => $city, 'url'=>$session_url, 'source'=>$session_source]);
            }
        }else{
            $url = Yii::$app->request->get('url');
            $source = Yii::$app->request->get('source');
            if(!$url || !$source){
                Yii::$app->session->set('url','https://www.thinkwithu.com');
                Yii::$app->session->set('source',1);
                return $this->renderPartial('registered',['country' => $country,'city' => $city]);
            }else {
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('registered',['url'=>$url,'source'=>$source,'country' => $country,'city' => $city]);
            }
        }
    }

    /**
     * 注册
     * @SEJAM
     */
    public function actionFound(){
        Yii::$app->session->set('phoneTime',time());
        $url = Yii::$app->session->get('url');
        $source = Yii::$app->session->get('source');
        if($url && $source){
            $url = Yii::$app->request->get('url');
            $source = Yii::$app->request->get('source');
            if($url && $source){
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('found');
            }else {
                return $this->renderPartial('found');
            }
        }else{
            $url = Yii::$app->request->get('url');
            $source = Yii::$app->request->get('source');
            if(!$url || !$source){
                die('<script>alert("路径错误，请重试");history.go(-1);</script>');
            }else {
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('found',['url'=>$url,'source'=>$source]);
            }
        }
    }




    /**
     * 邮箱注册
     */
    public function actionEmailRegister(){
        $ipInfo = Yii::$app->request->getUserIP();
        $data = Method::getIpInfo($ipInfo);
        $country = $data[0];
        $city = $data[1];
        if($country == '中国' && $city != '香港' && $city != '澳门' && $city != '台湾'){
            die('<script>alert("路径错误，请重试");history.go(-1);</script>');
        }
        Yii::$app->session->set('phoneTime',time());
        $url = Yii::$app->session->get('url');
        $source = Yii::$app->session->get('source');
        if($url && $source){
            $url = Yii::$app->request->get('url');
            $source = Yii::$app->request->get('source');
            if($url && $source){
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('emailRegister');
            }else {
                return $this->renderPartial('emailRegister');
            }
        }else{
            $url = Yii::$app->request->get('url');
            $source = Yii::$app->request->get('source');
            if(!$url || !$source){
                //默认 来源 gmat
                Yii::$app->session->set('url','https://www.thinkwithu.com');
                Yii::$app->session->set('source',1);
                return $this->renderPartial('emailRegister');
                // die('<script>alert("路径错误，请重试");history.go(-1);</script>');
            }else {
                Yii::$app->session->set('url',$url);
                Yii::$app->session->set('source',$source);
                return $this->renderPartial('emailRegister');
            }
        }
    }


}
